package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGoupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrResVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 属性分组
 *
 * @author zhuzhu
 * @email 838759460@qq.com
 * @date 2024-04-28 23:03:03
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 拿到该分类下的所有分组列表
     */
    @RequestMapping("/list/{catId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catId")Long catId){
        //PageUtils page = attrGroupService.queryPage(params);
        //你现在是要把该分类下的所有分组查出来，没具体指明查哪个分类的话，就查出所有分组
        PageUtils page=attrGroupService.queryPageByCatIdv1(params,catId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //拿到这个分组属于的三级分类id
        Long catelogId = attrGroup.getCatelogId();
        Long[]catelogPath=categoryService.findCurrentcatelogIdOfPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }
    @GetMapping("{attrgropId}/attr/relation")
    public R getAttrByGroupId(@PathVariable("attrgropId")Long attrgropId){
       List<AttrEntity> list=attrGroupService.listAttrByGroupId(attrgropId);
       return R.ok().put("data",list);
    }
    @PostMapping("attr/relation/delete")
    public R deleteAttrGroupRelation(@RequestBody AttrGoupRelationVo []attrGoupRelationVos){
        List<AttrGoupRelationVo> relationVos = Arrays.asList(attrGoupRelationVos);
        //attrGroupService.removeByAttrIdAndGroupId(relationVos);
        attrGroupService.removeByAttrIdAndGroupIdV1(relationVos);
        return R.ok();
    }

    /**
     * 该方法用于添加attr和分组的管理关系
     * @param attrGoupRelationVos
     * @return
     */
    @PostMapping("attr/relation")
    public R addAttrGroupRelation(@RequestBody AttrGoupRelationVo []attrGoupRelationVos){
        List<AttrGoupRelationVo> relationVos = Arrays.asList(attrGoupRelationVos);
        attrGroupService.saveAttrGroupRelation(relationVos);
        //attrGroupService.removeByAttrIdAndGroupId(relationVos);
        //attrGroupService.removeByAttrIdAndGroupIdV1(relationVos);
        return R.ok();
    }
    /**
     * 前端要给该分组关联属性，原则就是查出该分组属于的那个分类下，还未被该分类下其他分组关联的属性。
     * @param params  前端的url请求参数封装
     * @param attrgroupId 该分组Id
     * @return
     */
    @GetMapping("{attrgroupId}/noattr/relation")
    public R getNoRelationsOfThisGroup(@RequestParam Map<String, Object> params,
                                       @PathVariable("attrgroupId")Long attrgroupId){
        PageUtils page=attrGroupService.queryNoRelationsOfThisGroupV1(params,attrgroupId);
        return R.ok().put("page", page);
    }
    @GetMapping("{catelogId}/withattr")
    public R getAttrAndGroupByCategoryId(@PathVariable("catelogId")Long catelogId){
        List<AttrGroupWithAttrResVo>list=attrGroupService.queryAttrAndGroupByCategoryIdV1(catelogId);
        return R.ok().put("data",list);
    }
}
